<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

//积分表
class Integral extends Admin
{
//1=>产品代理积分,2=>租赁代理积分,3=>直推商家代理,4=>直推合伙人,5=>下级商户租赁代理业绩分成,
//6=>产品代理平级积分,7=>产品代理互助积分,8=>租赁代理平级积分,9=>租赁代理互助积分,10=>会员权益积分,
//11=>代理权益积分,12=>商家代理租赁积分,13=>服务中心租赁积分,14=>服务中心权益积分
    public function index()
    {
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            //开始结束时间
            $where['time'] = $post['time']??'';
            //状态
            $where['source'] = $post['source']??'';
            $wheres =[];
            if($where['time']){
                list($first,$last) = explode('至',$where['time']);
                $wheres[] = ['create_time','>',trim($first)];
                $wheres[] = ['overtime','<',trim($last)];
            }
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                $list = model($this->request->controller())->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            $list = model($this->request->controller())->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model($this->request->controller())->select()->toArray());
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);

        //产出总积分
        $zjf = model($this->request->controller())->where('status','=','1')->select()->toArray();
        $zjfs = 0;
        if($zjf){
            foreach ($zjf as $v){
                $zjfs = $zjfs + $v['num'];
            }
            $home['zjf'] = $zjfs;
        }else{
            $home['zjf'] = 0;
        }
        //用户积分总余额
        $zjyh = model($this->request->controller())->field('num')->where('source','=','16')->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['yhjf'] = $zjyhs;
        }else{
            $home['yhjf'] = 0;
        }
        //产品兑换积分
        $zjyh = model('integraldetailed')->field('num')->where('status','=','1')->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['cpjf'] = $zjyhs;
        }else{
            $home['cpjf'] = 0;
        }
        //公司证券
        $zjyh = model('integraldetailed')->field('num')->where('status','=','2')->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['gsjf'] = $zjyhs;
        }else{
            $home['gsjf'] = 0;
        }
        //sto
        $zjyh = model('integraldetailed')->field('num')->where('status','=','4')->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $V){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['sto'] = $zjyhs;
        }else{
            $home['sto'] = 0;
        }
        //设备维养
        $zjyh = model('integraldetailed')->field('num')->where('status','=','5')->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['seb'] = $zjyhs;
        }else{
            $home['seb'] = 0;
        }
        //贷款额度积分
        $zjyh = model('integraldetailed')->field('num')->where('status','=','3')->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['dkjf'] = $zjyhs;
        }else{
            $home['dkjf'] = 0;
        }
        $this->assign('home',$home);



        return $this->fetch();
    }
    public function user()
    {
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $id = $get['battery_order'];
        if(!$id){
            return '没有参数';
        }else{
            $this->assign('id',$id);
        }
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            $where['user_id'] = $id;
            //开始结束时间
            $where['time'] = $post['time']??'';
            //类型
            $where['source'] = $post['source']??'';
            $wheres =[];
            if($where['time']){
                list($first,$last) = explode('至',$where['time']);
                $wheres[] = ['create_time','>',trim($first)];
                $wheres[] = ['overtime','<',trim($last)];
            }
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                $list = model($this->request->controller())->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                $list = model($this->request->controller())->where('user_id','=',$id)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            $list = model($this->request->controller())->where('user_id','=',$id)->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model($this->request->controller())->select()->toArray());
        }
        //产品兑换积分
        $zjyh = model('integraldetailed')->field('num')->where([['status','=','1'],['user_id','=',$id]])->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['cpjf'] = $zjyhs;
        }else{
            $home['cpjf'] = 0;
        }
        //公司证券
        $zjyh = model('integraldetailed')->field('num')->where([['status','=','2'],['user_id','=',$id]])->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['gsjf'] = $zjyhs;
        }else{
            $home['gsjf'] = 0;
        }
        //sto
        $zjyh = model('integraldetailed')->field('num')->where([['status','=','4'],['user_id','=',$id]])->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $V){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['sto'] = $zjyhs;
        }else{
            $home['sto'] = 0;
        }
        //设备维养
        $zjyh = model('integraldetailed')->field('num')->where([['status','=','5'],['user_id','=',$id]])->select()->toArray();
        $zjyhs = 0;
        if($zjyh){
            foreach ($zjyh as $v){
                $zjyhs = $zjyhs + $v['num'];
            }
            $home['seb'] = $zjyhs;
        }else{
            $home['seb'] = 0;
        }
        //贷款额度积分
        $home['dkjf'] = 0;
        $this->assign('home',$home);


        $get = input('get.');
        if(isset($get['battery_order'])){
            $this->assign('id',$get['battery_order']);
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    public function views()
    {
        if($this->request->isPost()){
            $post = input('post.');
            $model = model('integral')->where('id','=',$post['id'])->with('order,integraldetailed')->find()->toArray();
            return $model;
        }
    }
}
